package com.android.pagemarket.ui.activity.trend;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.ContentManager;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.business.model.Message;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.ui.widget.CallPopWindow;
import com.android.pagemarket.ui.widget.TitleBar;
import com.android.pagemarket.util.ImageLoader;
import com.android.pagemarket.util.ImageLoader.Imager;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 动态详情
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class TrandDetailActivity extends TemplateActivity implements OnClickListener
{
    private Message message;
    private Imager  mImage;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if(!handIntent())
        {
            finish();
            return;
        }
        setContentView(R.layout.activity_tranddetail);
        initViews();
        loadData();
    }
    
    @Override
    protected void onPause()
    {       
        super.onPause();
    }
    
    @Override
    protected void onDestroy() 
    {
        if(mImage != null) ImageLoader.getInstance().clearBmp(mImage);
        super.onDestroy();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(message.type == Message.TYPE_ALERTS ? R.string.title_trand : R.string.title_promote);
        titleBar.setRightVisible(false);
    }
    
    @Override
    protected void setupBottomBar(LinearLayout bottomBar)
    {
        if(message.type == Message.TYPE_PROMOT && message.quotCall != null)
        {
            bottomBar.addView(inflate(R.layout.view_order_call_bottom), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));            
        }
    }
    
    private void loadData()
    {
        ContentManager.getInstance().updateRead(message);
    }
    
    private void initViews()
    {
        ImageView image = getViewById(R.id.image);
        TextView desc = getViewById(R.id.desc); 
        TextView title = getViewById(R.id.title);
        
        LayoutParams lp = image.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        image.setLayoutParams(lp);
        image.setMaxHeight(-1);
        image.setScaleType(ScaleType.FIT_XY);
        
        if(message.imageURL != null)
        {
            Display dd = getWindowManager().getDefaultDisplay();
            int maxWidth = dd.getWidth()-16;
            mImage = new Imager(message.imageURL, image, maxWidth, R.drawable.ic_pic_default);
            ImageLoader.getInstance().DisplayImage(mImage);
        }else
        {
            image.setVisibility(View.GONE);
        }
        desc.setText(message.content);
        title.setText(message.title);
        title.setVisibility(View.VISIBLE);
    }
    
    @Override
    public void onClick(View v)
    {
        if(getPopupWin() == null)
        {
            CallPopWindow pop = new CallPopWindow(this);
            String phone = message.quotCall;
            if(message.quotCall == null)
            {
                // 用默认的
                phone = InitManager.getInstance().getOfferPhone();
            }
            pop.setPhoneText(phone);
            pop.setCallPhone(phone);
            setPopWin(pop);
        }
        getPopupWin().showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }
    
    private boolean handIntent()
    {
        Intent it = getIntent();
        message = (Message)it.getSerializableExtra(KEY_PARAM);
        return message != null;
    }
    
    public static void startActivity(Context context, Message message)
    {
        Intent it = new Intent(context, TrandDetailActivity.class);
        it.putExtra(KEY_PARAM, message);
        context.startActivity(it);
    }
}
